//
//  ViewController.swift
//  Frame_Bounds
//
//  Created by 李煜 on 2023/3/30.
// https://blog.csdn.net/lihao_ios/article/details/107540979

import UIKit

class ViewController: UIViewController {
  override func viewDidLoad() {
    super.viewDidLoad()

    demo3()
  }

  /// 造view
  func makeView(frame: CGRect, color: UIColor) -> UIView {
    let oneView = UIView(frame: frame)
    oneView.backgroundColor = color
    return oneView
  }

  func printView(name: String, oneView: UIView) {
    print(name + ",frame: ", oneView.frame, "bounds: ", oneView.bounds)
  }

  func printFrameBounds(superView: UIView, subView: UIView) {
    print("v1 frame & bounds: ", superView.frame, superView.bounds)
    print("subView frame & bounds: ", subView.frame, subView.bounds)
  }

  func demo1() {
    let view01 = makeView(frame: .init(x: 100, y: 100, width: 50, height: 50), color: .gray)
    view.addSubview(view01)

    printView(name: "view01", oneView: view01)

    UIView.transition(with: view01, duration: 2, options: .curveEaseInOut) {
      view01.transform = CGAffineTransformMakeRotation(Double.pi / 4)
    } completion: { finish in
      if finish {
        self.printView(name: "view01", oneView: view01)
      }
    }
  }

  func demo2() {
    // 2.把view02_sub放到view02中,并且改变view02的bounds,会发生什么？
    let view02 = makeView(frame: .init(x: 100, y: 100, width: 200, height: 200), color: .init(white: 0.851, alpha: 1))
    view.addSubview(view02)

    let view02_sub = makeView(frame: .init(x: 0, y: 0, width: 60, height: 60), color: .init(red: 1, green: 0.89, blue: 0.552, alpha: 1))

    view02.addSubview(view02_sub)

    printFrameBounds(superView: view02, subView: view02_sub)

    UIView.animate(withDuration: 2) {
      view02.bounds = CGRectMake(-30, -30, 200, 200)
    } completion: { _ in
      view02.addSubview(view02_sub)
    }
  }

  func demo3() {
    // 3.当父View的frame改变的时候，子View会发生什么变化？
    let superView = makeView(frame: .init(x: 100, y: 100, width: 200, height: 200), color: .orange)
    let sonView = makeView(frame: .init(x: 0, y: 0, width: 60, height: 60), color: .gray)

    view.addSubview(superView)
    superView.addSubview(sonView)

    printFrameBounds(superView: superView, subView: sonView)

    UIView.animate(withDuration: 2) {
      superView.frame = .init(x: 0, y: 250, width: 150, height: 150)

    } completion: { _ in
      self.printFrameBounds(superView: superView, subView: sonView)
    }
  }
}
